package com.junxonline.plat.controller.personnel;

import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.personnel.PersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: PersonnelController
 * @Description: 人员控制器
 * @author JunX
 */

@RestController
@RequestMapping(value = "/admin/personnel")
public class PersonnelController extends BaseController {

    @Autowired
    private PersonnelService personnelService;

    /**
     * 获取人员数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getPersonnel")
    public ResultVO getPersonnel(@RequestBody QueryHouseholdVO vo) {

        if (CommonUtils.isEmpty(vo.getHouseholdId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(personnelService.getHousehold(vo));
    }

    /**
     * 筛选查询人员数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryPersonnel")
    public PageResultVO queryPersonnel(PageVO page, QueryHouseholdVO vo) {
        return getReturnSuccess(personnelService.queryHousehold(page, vo));
    }


    /**
     * 添加人员
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addPersonnel")
    public ResultVO addPersonnel(@RequestBody HouseholdVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getHouseholdName()) || CommonUtils.isEmpty(vo.getHouseholdSex()) ||
            CommonUtils.isEmpty(vo.getHouseholdAge()) || CommonUtils.isEmpty(vo.getHouseholdIDCard())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = personnelService.addHousehold(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改人员
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editPersonnel")
    public ResultVO editPersonnel(@RequestBody HouseholdVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getHouseholdId()) || CommonUtils.isEmpty(vo.getHouseholdName()) ||
            CommonUtils.isEmpty(vo.getHouseholdSex()) || CommonUtils.isEmpty(vo.getHouseholdAge()) || CommonUtils.isEmpty(vo.getHouseholdIDCard())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = personnelService.editHousehold(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除人员
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delPersonnel")
    public ResultVO delPersonnel(@RequestBody HouseholdIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] personnelIdsArray = vo.getHouseholdIds();

        boolean successMenu = personnelService.delHousehold(personnelIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

}


